# -*- encoding: utf-8 -*-
import cv2
import math
import numpy as np


class RectImg(object):
    def __init__(self, w, h):
        self.left_x, self.left_y = w + 100, h + 100
        self.right_x, self.right_y = 0, 0
        self.xp = 0
        self.yp = 0
        self.w = w
        self.h = h
        # 画布
        self.imgCanvas = np.zeros((h, w, 3), np.uint8)
        self.zero = np.zeros((h, w, 3), np.uint8)
        self.start_x = 0
        self.start_y = 0
        self.point_len = 0
        self.show_rect = False
        self.img_reco_crop = None

    def clean(self):
        self.point_len = 0
        self.start_x, self.start_y = 0, 0
        self.left_x, self.left_y = self.w + 100, self.h + 100
        self.right_x, self.right_y = 0, 0
        self.xp = 0
        self.yp = 0
        self.show_rect = False
        self.imgCanvas = cv2.bitwise_and(self.imgCanvas, self.zero)
        self.img_reco_crop = None

    def point_xy(self, x, y):
        if self.left_x > x:
            self.left_x = x

        if self.left_y > y:
            self.left_y = y

        if self.right_x < x:
            self.right_x = x

        if self.right_y < y:
            self.right_y = y

        # print(x, self.left_x, self.right_x, y, self.left_y, self.right_y)

    def get_rect(self, img):
        if not self.show_rect:
            return None, None
        if self.right_y <= self.left_y or self.left_x >= self.right_x:
            return None, None
        # print(self.left_x, self.right_x)
        roi = img[self.left_y:self.right_y, self.left_x:self.right_x]
        roi = roi.copy()
        w = roi.shape[0]
        h = roi.shape[1]
        if w > 150:
            w = 150
        if h > 150:
            h = 150
        self.img_reco_crop = roi
        if roi.shape[0] != w or roi.shape[1] != h:
            self.img_reco_crop = cv2.resize(roi, (150, 150))  # 待识别区域块
        return roi, self.img_reco_crop

    def show_img(self, img):
        roi, img_reco_crop = self.get_rect(img)
        imgGray = cv2.cvtColor(self.imgCanvas, cv2.COLOR_BGR2GRAY)
        _, imgInv = cv2.threshold(imgGray, 50, 255, cv2.THRESH_BINARY_INV)
        imgInv = cv2.cvtColor(imgInv, cv2.COLOR_GRAY2BGR)
        img = cv2.bitwise_and(img, imgInv)
        img = cv2.bitwise_or(img, self.imgCanvas)
        return img, roi, img_reco_crop

    def draw_rect(self, img):
        if self.img_reco_crop is None:
            return img
        rc = self.img_reco_crop
        ty1 = int(img.shape[0] - rc.shape[0] - 5)
        ty2 = int(img.shape[0] - 5)
        tx1 = int(img.shape[1] - rc.shape[1] - 5)
        tx2 = int(img.shape[1] - 5)
        img[ty1:ty2, tx1:tx2] = rc
        cv2.rectangle(img, (tx1, ty1), (tx2, ty2), (0, 0, 255), thickness=2)
        return img

    # 绘制矩形
    def draw(self, img, x, y):
        if self.show_rect:
            return img

        self.point_xy(x, y)
        if self.xp == 0 and self.yp == 0:
            self.xp, self.yp = x, y

        # print(self.point_len, math.fabs(self.start_x - x), math.fabs(self.start_y - y))
        if self.start_x == 0 and self.start_y == 0:
            self.start_x, self.start_y = x, y
        elif self.point_len > 100:
            # 绘制满足条件
            if math.fabs(self.start_x - x) < 10 and math.fabs(self.start_y - y) < 10:
                self.show_rect = True

        cv2.line(self.imgCanvas, (self.xp, self.yp), (x, y), color=(0, 0, 255), thickness=5)
        self.xp, self.yp = x, y
        self.point_len = self.point_len + 1
        return img
